export const parseTime = (date, format = '{y}-{m}-{d} {h}:{i}:{s}') => {
  if (date) {
    const temp = new Date(date)
    const timeFormat = format.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
      let value = temp.getFullYear()
      if (key === 'm') {
        value = temp.getMonth() + 1
      } else if (key === 'd') {
        value = temp.getDate()
      } else if (key === 'h') {
        value = temp.getHours()
      } else if (key === 'i') {
        value = temp.getMinutes()
      } else if (key === 's') {
        value = temp.getSeconds()
      }
      if (result.length > 0 && value < 10) {
        value = '0' + value
      }
      return value || 0
    })
    return timeFormat
  } else {
    return ''
  }
}

/**
 * 获取uuid
 */
export const getUUID = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
  })
}

export const goToAnchor = (id) => {
  var anchor = document.getElementById(id)
  // chrome
  document.body.scrollTop = anchor.offsetTop
  // firefox
  document.documentElement.scrollTop = anchor.offsetTop
  // safari
  window.pageYOffset = anchor.offsetTop
}

export const getQueryObject = (url) => {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}

export const getQueryString = (name) => {
  return getQueryObject(window.location.href)[name]
}

export const getLocal = (name) => {
  return localStorage.getItem(name)
}

export const setLocal = (name, value) => {
  localStorage.setItem(name, value)
}

export const addDay = (days) => {
  //创建date
  let nowDate = new Date()
  //添加天数
  nowDate.setDate(nowDate.getDate() + days)
  //返回
  return nowDate
}

export const addMonth = (months) => {
  //创建date
  let nowDate = new Date()
  //添加周数
  nowDate.setMonth(nowDate.getMonth() + months)
  //返回
  return nowDate
}

export const addYear = (years) => {
  //创建date
  let nowDate = new Date()
  //添加年数
  nowDate.setMonth(nowDate.getYear() + years)
  //返回
  return nowDate
}

export const dateFormat = (fmt, date) => {
  let ret
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (let k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
      )
    }
  }
  return fmt
}

/**
 * Parse the time to string
 */
export function parse(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string') {
      if (/^[0-9]+$/.test(time)) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time
          .replace(new RegExp(/-/gm), '/')
          .replace('T', ' ')
          .replace(new RegExp(/\.[\d]{3}/gm), '')
      }
    }

    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }

  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}
